//
//  TechnologyViewController.m
//  HomePage
//
//  Created by xiong qi on 13-5-31.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "TechnologyViewController.h"

@implementation TechnologyViewController
-(id)initWithFrame:(CGRect)frame
{
    self = [super init];
    if (self) {
        self.view.frame = frame;
        
        
        UIImageView * background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,1024, 768)];
        [self.view addSubview:background];
        [background release];
        [background setImage:[UIImage imageNamed:@"AllAuto_Image_BG"]];
        
        UIButton *  backBtn = [[UIButton alloc]initWithFrame:CGRectMake(50, 18, 55,40)];
        [backBtn setImage:[UIImage imageNamed:@"home_button"] forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(onBack:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:backBtn];
        [backBtn release];
        
        
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 25, 400, 35)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.font = [UIFont fontWithName:@"Arial" size:30];
        titleLabel.textColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0];
        [self.view addSubview:titleLabel];
        [titleLabel release];
        [titleLabel setText:@"广汽科技"];
        
        UIImageView * lineView = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-658)/2,70, 658, 8)];
        lineView.image = [UIImage imageNamed:@"AllAuto_Config_TimeLine_BG"];
        [self.view addSubview:lineView];
        [lineView release];
        
        
        
//
//        brandDetailView = [[BrandDetailView alloc] initWithFrame:CGRectMake(0, 110, frame.size.width, frame.size.height-110) Root:path];
//        [self.view addSubview:brandDetailView];
//        [brandDetailView release];
        
        int width = (TemplateSpaceX+TemplateWidth)*3;
        int height = (TemplateSpaceY+TemplateHeight)*2+TemplateTopSpace;
        
        NSString * rootpath = [self GetRootPath];
        NSArray * array = [self GetFiles:rootpath];
        
        timelineView = [[TimeLineView alloc] initWithFrame:CGRectMake(12, 683, 1000, 80)];
        [self.view addSubview:timelineView];
        [timelineView release];
        timelineView.alpha = 0;
        
        transView = [[TransmutationView alloc] initWithFrame:CGRectMake((frame.size.width-width)/2, (frame.size.height-height)/2, width, height) RootPath:rootpath Files:array];
        [self.view addSubview:transView];
        [transView release];
        transView.parentView = self;
        [transView renderTemplate];
        
        [transView addObserver:self forKeyPath:@"currentMiddleView" options:NSKeyValueObservingOptionNew context:nil];
        
    }
    return self;
}

-(void)onBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
	//实现相关操作
	UIView * currentview = [change objectForKey:NSKeyValueChangeNewKey];
    if (currentview != nil) {
        [self SetTimeLineIndex:currentview.tag-100];
    }
    
}

-(NSString *)GetRootPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [NSString stringWithFormat:@"%@/paddata/company/Technology",[paths objectAtIndex:0]];
    return path;
}

-(NSArray *)GetFiles:(NSString *)path
{
    return [[ResourceLoader getInstance] getTemplatesByPath:path];
}

-(void)RefreshTimeLine:(NSArray *)array
{
    [timelineView RefreshByFavorite:array];
}

-(void)SetTimeLineAlpha:(NSNumber *)alpha
{
    timelineView.alpha = [alpha floatValue];
}

-(void)SetTimeLineIndex:(int)index
{
    [timelineView SetShowIndex:index];
}


-(void)dealloc
{
    [transView removeObserver:self forKeyPath:@"currentMiddleView"];
    [super dealloc];
}


@end
